package org.brisling.ethic.util;

import java.util.Calendar;
import java.util.Map;

import org.brisling.common.SysConfig;
import org.brisling.common.util.JsonUtil;
import org.brisling.common.util.LongUtil;
import org.brisling.common.util.MapUtil;
/**
 * 
 * @author jacksonWang
 *
 */
public class EthicUtil {
	
	private static EthicUtil eu = null;
	private EthicUtil(){}
	
	public static EthicUtil getInstance(){
		if(eu==null){
			eu = new EthicUtil();
		}
		return eu;
	}
	
	/**
	 * 获取当前考评年度信息
	 * @return
	 */
	public static String getEvaluateYear(){
		
		
		String end	= "9-30";
		
		//获取默认年份
		String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
		
		//获取考评周期配置信息
		String _config = SysConfig.getConfig("ethic_date_config");
		
		if(_config!=null){
			//解析考评周期配置信息
			Map<String,Object> map = JsonUtil.body2Class(_config, Map.class);
			
			
			if(map.containsKey("end_date")){
				end = MapUtil.getMapData(map, "end_date");
			}
			
			String arr[] = end.split("-");
			//是否存在年份调整信息
			if(LongUtil.getIntValue(arr[0], 0)<=6){
				
				
				Integer _year = LongUtil.getIntValue(year, null);			
				
				_year -=1;
				
				year = String.valueOf(_year);
			}
		}
		return year;
	}
	//获取医德考评评价起始时间
		public static String[] getEvaluateDate(String year){
			String start = "10-1";
			String end	= "9-30";
			
			year = year!=null?year:String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
			
			String _config = SysConfig.getConfig("ethic_date_config");
			
			String[] ret = new String[2];
			
			if(year!=null){
				Integer _year = LongUtil.getIntValue(year, null);
				ret[0] = _year+"-"+start;
				ret[1] = (_year+1) +"-"+end;
			}
			
			if(_config!=null){
				Map<String,Object> map = JsonUtil.body2Class(_config, Map.class);
				
				if(map!=null){
					if(map.containsKey("start_date")){
						start = MapUtil.getMapData(map, "start_date");
					}
					if(map.containsKey("end_date")){
						end = MapUtil.getMapData(map, "end_date");
					}
					
					
					Integer _startyear = null;
					Integer _endyear = null;
					//起始年份的确定
					if(map.containsKey("year_change")){
						String change = MapUtil.getMapData(map, "year_change");
						
						_startyear = LongUtil.getIntValue(year, null);
						
						
						if(change!=null){
							Integer _change = LongUtil.getIntValue(change, null);
							if(_startyear!=null  ){
								
								//判断年度调整值是否存在
								if(_change!=null){
									_startyear +=_change;								
								}
								_endyear = _startyear +1;
								
								ret[0] = _startyear +"-"+start;;
								ret[1] = _endyear +"-"+end;;
							}
						}				
						
					}
				}
			}
			
			return ret;
		}
	

}
